package com.code.service;

import com.code.constant.ErrorEnum;
import com.code.dao.IHistoryLineDao;
import com.code.entity.HistoryLine;
import com.code.util.ResultData;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 历史时间线服务类
 *
 * @author wwei
 * @date 2019/8/7
 */
@Service
public class HistoryService {

    private final IHistoryLineDao iHistoryLineDao;

    public HistoryService(IHistoryLineDao iHistoryLineDao) {
        this.iHistoryLineDao = iHistoryLineDao;
    }

    /**
     * 保存个人的历史线记录
     * Created by wwei on 2019/8/7 22:45
     * @param historyLine 时间线数据
     * @return 保存结果
     */
    public ResultData saveHistory(HistoryLine historyLine) {
        if ( StringUtils.isEmpty(historyLine.getId())) {
            LocalDateTime now = LocalDateTime.now();
            String nowTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            String username = "wwei";
            historyLine.setTimestamp(nowTime);
            historyLine.setUpdateTime(nowTime);
            historyLine.setCreateTime(nowTime);

            historyLine.setUpdateUser(username);
            historyLine.setCreateUser(username);
            iHistoryLineDao.save(historyLine);
            return ResultData.success("新增成功");
        }
        return ResultData.error(ErrorEnum.ERROR);
    }

    /**
     * 获取用户历史时间线记录
     * @param userId 用户id
     * @return 记录
     */
    public ResultData getHistory(Long userId) {
        if (null == userId) {
            return ResultData.error(ErrorEnum.ERROR);
        }
        return ResultData.success(iHistoryLineDao.findByUserId(userId));
    }
}
